<?php

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.bookings.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.suppliers.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.SimpleEmailService.php";
require_once SITE_ROOT_PATH."/_lib/classes/controller/BookingHotelController.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotel_supplier_low.php";

    function send_notification_email($to) {
        
            $voucher_html = get_include_contents(SITE_ROOT_PATH."_lib/html/accommodation_voucher.php");   
            $invoice_html = get_include_contents(SITE_ROOT_PATH."_lib/html/accommodation_invoice.php");   
            
            include SITE_ROOT_PATH."/_lib/classes/sendgrid/SendGrid_loader.php";        
            $sendgrid = new SendGrid(SEND_GRID_USER, SEND_GRID_PASSWORD);
            $mail_voucher = new SendGrid\Mail();            
            $mail_voucher->addTo($to)->
                addTo('juan@amigomedia.ie')-> 
                setFrom('info@latedeals.ie')->
                setSubject('Latedeals.ie Accomodation Voucher')->
                setHtml($voucher_html);                       
            $mail_invoice= new SendGrid\Mail();            
            $mail_invoice->addTo($to)->
                addTo('juan@amigomedia.ie')-> 
                setFrom('info@latedeals.ie')->
                setSubject('Latedeals.ie Accomodation Invoice')->
                setHtml($invoice_html);                               
            $sendgrid->smtp->send($mail_voucher);
            $sendgrid->smtp->send($mail_invoice);            
 

               
    }


    function preprocess_data($data) {
        // bring rooms adults, children and rates to array's top
        $i = 1;
        while ($i <= $data["rooms_count"]) {
            $data["adults_{$i}"]    = $data["room_sets"][$i]["adults"];
            $data["child_{$i}"]     = $data["room_sets"][$i]["children"];

            // access room id by stored hotel and room ids
            $roomId = $_SESSION['booking']['room_ids'][$i];

            $roomParams = $data["room_sets"][$i]["rooms"][$roomId];
            // user original room_rate from supplier, without additional markup
            $data["room_{$i}_rate"] = $roomParams["RateOrig"];

            // RID - room id collecting; RID, RID_2, RID_3 - no index for first room!
            // mapping is made by booking.csv
            $data["room_id_{$i}"] = $roomId;

            $i++;
        }
        return $data;
    }
    
    
    
    // Return error, message, booking_reference
    
    function process_booking(){
       
        session_start();

        $result = array ("error" => 0, "message" => "", "booking_ref" => 0);


        // avoid time limit
        set_time_limit(0);

        // check for search results and booking timeouts (see: common.php)
        if($location = check_timeouts()) {
            header("Location: $location");
            exit;
        }
        //print_r($_SESSION); die();
        $results = $_SESSION["hotels_search_results"]["data"];
        $hotel = $_SESSION["booking"]["hotel"];
        $hotel_id = $_SESSION["booking"]["hotel_id"];
        $booking_details = $_SESSION["booking"]["details"];
        $payment = $_SESSION["booking"]["payment"];
        $room_ids = $_SESSION["booking"]["room_ids"];
        $params = array_merge($booking_details, $payment);
        $params["supplier_code"] = $hotel["supplier_code"];
        
        // @FIXME: This is a hot fix, must be fixed correctly
        if (!$params['total_currency']) {
            $params['total_currency'] = 'EUR';    
        }
        // end hot fix
        $booking = new bookings();
        $booking->set_properties($params);
        $data = array_merge($results, $booking_details, $payment, $hotel, array('room_ids' => $room_ids, 'hotel_id'=>$hotel_id));
        $data = preprocess_data($data);
        $suppliers = new suppliers();

        $bookingHotelController = new BookingHotelController("low");

        $booking_result = $bookingHotelController->executeActionBooking($data);

        if ($booking_result["success"]=="true") {
            $booking_id = $booking->complete_booking($booking_result);
            $settleResult = $booking->settle_payment_realex();
            if ($settleResult['settle_void_result']!==0) {
                $result['error'] = 1;
                $result['message'] = "Problems to process your booking, please contact us to do it";  //$booking_result["error_msg"];            
                //$suppliers->cancel_booking($data);
            }
            //$data = array_merge($data, $booking_result);
            $result['error'] = 0;
            $result['booking_ref'] = $booking_result["booking_ref"];

        } else {
                // booking failure, cancel CC transaction
            $booking->void_payment_realex($booking_result["error_msg"]);
            $result['error'] = 1;
            $result['message'] = "Problems to process your booking, please contact us to do it";  //$booking_result["error_msg"];

        }

        $_SESSION["booking"]["result"] = $result;

        return $result;
        
    }
    
    function build_room_details_to_voucher($booking_rooms){
        $div = '';
        foreach ($booking_rooms as $key => $room) {
            $div = $div . ' <tr>
                                <td width="210" valign="top" align="left"><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000"><b>Room ' . $key + 1 .'</b></span></td>
                                <td width="370" valign="top" align="left"><span  style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000">'. $room['room_type'] .'</span></td>
                            </tr>
                            <tr>
                                <td width="210" valign="top" align="left"><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000"><b>Board</b></span></td>
                                <td width="370" valign="top" align="left"><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000">'. $room['board_basis_code'] .'</span></td>
                            </tr>
                            <tr>
                                <td width="210" valign="top" align="left"><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000"><b>Occupancy</b></span></td>
                                <td width="370" valign="top" align="left">
                                    <table width="200" cellspacing="0" cellpadding="2" border="0">
                                        <colgroup>
                                            <col width="80" />
                                            <col width="100" />
                                        </colgroup>
                                        <tr>
                                            <td width="80" valign="top" align="left"><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000"><b>Adults</b></span></td>
                                            <td width="100" valign="top" align="left"><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000">' .$room['num_adult'] .'</span></td>
                                        </tr>
                                        <tr>
                                            <td width="80" valign="top" align="left"><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000"><b>Children</b></span></td>
                                            <td width="100" valign="top" align="left"><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif; color:#000">'. $room['num_child']. '</span></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>';                        
        }
        return $div;
    }
    
    /*
    function send_email_by_sendgrid($to, $from, $subject, $html, $subject2,$html2){
        include SITE_ROOT_PATH."/_lib/classes/sendgrid/SendGrid_loader.php";        
        $sendgrid = new SendGrid(SEND_GRID_USER, SEND_GRID_PASSWORD);
        $mail_voucher = new SendGrid\Mail();            
        $mail_voucher->addTo($to)->
               addTo('info@latedeals.ie')-> 
               setFrom($from)->
               setSubject($subject2)->
               setHtml($html2);                       
        $mail_invoice= new SendGrid\Mail();            
        $mail_invoice->addTo($to)->
               addTo('info@latedeals.ie')-> 
               setFrom($from)->
               setSubject($subject)->
               setHtml($html);                               
       $sendgrid->smtp->send($mail_voucher);
       $sendgrid->smtp->send($mail_invoice);

    }*/
    
